<?php

    /**
     * @package      Modules
     * @subpackage   Core_Classes
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Classes_EventListener extends Core_Event_Listener
    {

    ////////////////////////////////////////////////////////////////////////////
    //                                 Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
        * Handles the userLoggedIn event
        *
        * @param        Core_Event      $event
        * @return       void
        */
        public function onUserLoggedIn(Core_Event $event)
        {
            $authNamespace = Core_Authentification::getNamespace();
            $admin = Doctrine_Query::create()
                        ->from('DbAdmin a')
                        ->leftJoin('a.DbProfile p')
                        ->leftJoin('p.DbModule m')
                        ->leftJoin('m.DbPackage pa')
                        ->where('a.id = ?', $authNamespace->id)
                        ->fetchOne();

            $authorized = array();

            foreach ($admin->DbProfile as $profile) {
                foreach ($profile->DbModule as $module) {
                    $package = $module->DbPackage;

                    if (!array_key_exists($package->id, $authorized)) {
                        $authorized[$package->id]['package'] = $package;
                        $authorized[$package->id]['modules'] = array();
                    }

                    $authorized[$package->id]['modules'][$module->slug] = $module;
                }
            }

            $authNamespace->authorized = $authorized;
        }
    }
